<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2020/2/22
 * Time: 11:26
 */

namespace app\index\controller;


use app\admin\model\Comment;
use app\admin\model\News;
use app\admin\model\News as NewsModel;
use app\admin\model\NewsLunbo;
use app\admin\model\NewsUserComment;
use think\Db;
use think\Exception;
use think\facade\Request;

class Index extends Commen
{
    /**
     * 首页
     * @return \think\response\View
     * @throws \think\exception\DbException
     */
    public function index()
    {
        //轮播图
        $lunboList = NewsLunbo::getLunboNews(1,10,[],['a.id'=>'asc'],'b.title,b.id,b.abstract,b.event_image');
        $countImage = count($lunboList['data']);
        foreach ($lunboList['data'] as $k => $v)
        {
            if (strlen($v['event_image']) < 5) {
                $countImage--;
            }
        }
        $this->assign('lunboList',$lunboList['data']);
        $this->assign('countImage',$countImage);
        return view("index");
    }

    /**
     * 新闻分页列表
     * @return \think\response\View
     * @throws \think\exception\DbException
     */
    public function newsCateList()
    {
        $id = input("id");
        $cate_name = input("cate_name");

        //获取新闻列表
        $where = [];
        $where[] = ['cate_id','=',$id];

        //获取分页参数
        $pageOption = $this->getPageAndPageSize(input('page')?input('page'):1,20);
        $newsList = NewsModel::getPageListByPage($pageOption['page'],$pageOption['pageSize'],$where,['create_time'=>'desc'],'id,title,create_time');

        $countPage = $newsList['last_page'];  //总页数
        $currentPage = input('page')?input('page'):1;

        //最新6条文章
        $where = [];
        $where[] = ['is_del','=',1];
        $newsArticle = News::getLimitList($where,'id,title',['id'=>"desc"],'0','6');

        $this->assign("newsList",$newsList['data']);
        $this->assign("cate_name",$cate_name);
        $this->assign('countPage',$countPage);
        $this->assign('currentPage',$currentPage);
        $this->assign('newsArticle',$newsArticle);
        $this->assign('title',"");
        return view("news_cate_list");
    }
    /**
     * 新闻详情页面
     * @return \think\response\View
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function detail()
    {
        //拿取新闻信息
        $id = input("id");
        $where[] = ["id",'=',$id];
        $newInfo = NewsModel::getFieldValue("*",$where);
        $content=$newInfo['content'];
        if(get_magic_quotes_gpc()){
            $content=stripslashes($content);
        }
        $newInfo['content']=htmlspecialchars_decode($content);
        $this->assign("newInfo",$newInfo);

        //拿取 前十条评论 信息
        $where  = [];
        $where[]  =  ['news_id','=',$id];
        $where[]  =  ['status','=',1];
        $commmentList = Comment::getCommentByLimit('10',['id'=>'desc'],$where,'b.username,b.id,b.comment');

        //拿取最新6条新闻
        $where = [];
        $where[] = ['is_del','=',1];
        $newsArticle = News::getLimitList($where,'id,title',['id'=>"desc"],'0','6');

        $this->assign('newsArticle',$newsArticle);
        $this->assign('commmentList',$commmentList);
        return view("detail");
    }

    /**
     * 插入评论
     * @return array
     */
    public function comment()
    {
        if(Request::isAjax())
        {
            if(is_null(session("user_name")) && is_null(session("user_id")))
            {
                return ['code'=>2 ,'msg'=>'请先登录','data'=>['url'=>url('user/login')]];
            }
            $comment = input("comment");
            $news_id = input("news_id");

            try{
                Db::startTrans();

                //首先插入评论：
                $commentData = [];
                $commentData['comment'] = $comment;
                $commentData['username'] = session('user_name');
                $commentData['user_id'] = session("user_id");
                $commentData['create_time'] =  $commentData['create_time']  = time();
                $res = Comment::create($commentData);

                if($res !== false)
                {
                    //插入关系表
                    $map = [];
                    $map['comment_id'] = $res['id'];
                    $map['news_id'] = $news_id;
                    NewsUserComment::create($map);
                }
                Db::commit();
            }catch (Exception $e){
                Db::rollback();
                return ['code'=>1 ,'msg'=>'评论失败','data'=>[]];
            }
            return ['code'=>0 ,'msg'=>'评论成功','data'=>['user_name'=>session("user_name"),'comment'=>$comment]];
        }
    }

    /**
     * 搜索结果
     * @return \think\response\View
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function search()
    {
        $title = input("title");

        $where = [];
        $where[] = ['title',"like",'%'.$title.'%'];

        //获取分页参数
        $pageOption = $this->getPageAndPageSize(input('page')?input('page'):1,20);
        $newsList = NewsModel::getPageListByPage($pageOption['page'],$pageOption['pageSize'],$where,['create_time'=>'desc'],'id,title,create_time');

        $countPage = $newsList['last_page'];  //总页数
        $currentPage = input('page')?input('page'):1;

        //最新6条文章
        $where = [];
        $where[] = ['is_del','=',1];
        $newsArticle = NewsModel::getLimitList($where,'id,title',['id'=>"desc"],'0','6');

        $this->assign("newsList",$newsList['data']);
        $this->assign("cate_name",'搜索结果');
        $this->assign('countPage',$countPage);
        $this->assign('currentPage',$currentPage);
        $this->assign('newsArticle',$newsArticle);
        $this->assign('title',$title);
        return view("search_list");
    }
}